﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace EntityFramework6
{


    public class ClientContext : DbContext
    {
        public ClientContext()
            : base("Server=.;Database=Riz_XFramework;uid=sa;pwd=123456;pooling=true;max pool size=1;min pool size=1;connect timeout=10;")
        {
        }

        protected override void OnModelCreating(DbModelBuilder builder)
        {
            builder
            .Entity<Model.Rabbit>()
            .ToTable("Sys_Rabbit")
            .HasKey(x => x.DemoId);

            builder
            .Entity<Model.Client>()
            .ToTable("Bas_Client")
            .HasKey(x => x.ClientId);

            builder
            .Entity<Model.Account>()
            .ToTable("Bas_ClientAccount")
            .HasKey(x => new { x.ClientId, x.AccountId });


            builder
            .Entity<Model.AccountMarket>()
            .ToTable("Bas_ClientAccountMarket")
            .HasKey(x => new { x.ClientId, x.AccountId, x.MarketId });
        }
    }

    public class Model
    {
        public class Client
        {
            public Client()
            {

            }

            public Client(Client model)
            {

            }

            public int ClientId { get; set; }

            public string ClientCode { get; set; }

            public string ClientName { get; set; }

            public byte State { get; set; }

            public DateTime? ActiveDate { get; set; }

            public int CloudServerId { get; set; }

            public virtual ICollection<Account> Accounts { get; set; }
        }

        public class Account
        {

            public int ClientId { get; set; }

            public string AccountId { get; set; }

            public string AccountName { get; set; }

            public virtual Client Client { get; set; }

            public virtual ICollection<AccountMarket> AccountMarkets { get; set; }
        }

        public class AccountMarket
        {
            public int ClientId { get; set; }

            public string AccountId { get; set; }

            public int MarketId { get; set; }

            public string MarketCode { get; set; }

            public string MarketName { get; set; }
        }

        //[Table("Sys_Rabbit")]
        public partial class Rabbit
        {
            /// <summary>
            /// 初始化 <see cref="Demo"/> 类的新实例
            /// </summary>
            public Rabbit()
            {
                this.HookConstructor();
            }

            /// <summary>
            /// 初始化 <see cref="Demo"/> 类的新实例
            /// </summary>
            public Rabbit(int demoId, string demoName)
            {

            }

            /// <summary>
            /// 初始化 <see cref="Demo"/> 类的新实例
            /// </summary>
            public Rabbit(Rabbit model)
            {
                this.HookConstructor();
            }

            /// <summary>
            /// demoid
            /// </summary>
            //[Column(IsKey = true, IsIdentity = true)]
            public virtual int DemoId { get; set; }

            /// <summary>
            /// democode
            /// </summary>
            [MaxLength(32)]
            [Column("DemoCode", TypeName = "varchar")]
            public virtual string RizDemoCode { get; set; }

            /// <summary>
            /// demoname
            /// </summary>
            [MaxLength(32)]
            [Column(TypeName = "nvarchar")]
            public virtual string DemoName { get; set; }

            /// <summary>
            /// demoboolean
            /// </summary>
            public virtual bool DemoBoolean { get; set; }

            /// <summary>
            /// demoboolean_nullable
            /// </summary>
            public virtual Nullable<bool> DemoBoolean_Nullable { get; set; }

            /// <summary>
            /// demochar
            /// </summary>
            public virtual string DemoChar { get; set; }

            /// <summary>
            /// demochar_nullable
            /// </summary>
            public virtual string DemoChar_Nullable { get; set; }

            /// <summary>
            /// demobyte
            /// </summary>
            public virtual byte DemoByte { get; set; }

            /// <summary>
            /// demobyte_nullable
            /// </summary>
            public virtual Nullable<byte> DemoByte_Nullable { get; set; }

            /// <summary>
            /// demodate
            /// </summary>
            //[Column(DbType = System.Data.DbType.Date)]
            public virtual DateTime DemoDate { get; set; }

            /// <summary>
            /// demodate_nullable
            /// </summary>
            public virtual Nullable<DateTime> DemoDate_Nullable { get; set; }

            /// <summary>
            /// demodatetime
            /// </summary>
            public virtual DateTime DemoDateTime { get; set; }

            /// <summary>
            /// demodatetime_nullable
            /// </summary>
            public virtual Nullable<DateTime> DemoDateTime_Nullable { get; set; }

            /// <summary>
            /// demodatetime2
            /// </summary>   
            //[Column(DbType = System.Data.DbType.DateTime2, Scale = 6)]
            public virtual DateTime DemoDateTime2 { get; set; }

            /// <summary>
            /// demodatetime2_nullable
            /// </summary>
            public virtual Nullable<DateTime> DemoDateTime2_Nullable { get; set; }

            /// <summary>
            /// demodecimal
            /// </summary>
            public virtual decimal DemoDecimal { get; set; }

            /// <summary>
            /// demodecimal_nullable
            /// </summary>
            public virtual Nullable<decimal> DemoDecimal_Nullable { get; set; }

            /// <summary>
            /// demodouble
            /// </summary>
            public virtual double DemoDouble { get; set; }

            /// <summary>
            /// demodouble_nullable
            /// </summary>
            public virtual Nullable<double> DemoDouble_Nullable { get; set; }

            /// <summary>
            /// demofloat
            /// </summary>
            public virtual float DemoFloat { get; set; }

            /// <summary>
            /// demofloat_nullable
            /// </summary>
            public virtual Nullable<float> DemoFloat_Nullable { get; set; }

            /// <summary>
            /// demoguid
            /// </summary>
            public virtual Guid DemoGuid { get; set; }

            /// <summary>
            /// demoguid_nullable
            /// </summary>
            public virtual Nullable<Guid> DemoGuid_Nullable { get; set; }

            /// <summary>
            /// demoshort
            /// </summary>
            public virtual short DemoShort { get; set; }

            /// <summary>
            /// demoshort_nullable
            /// </summary>
            public virtual Nullable<short> DemoShort_Nullable { get; set; }

            /// <summary>
            /// demoint
            /// </summary>
            public virtual int DemoInt { get; set; }

            /// <summary>
            /// demoint_nullable
            /// </summary>
            public virtual Nullable<int> DemoInt_Nullable { get; set; }

            /// <summary>
            /// demolong
            /// </summary>
            public virtual long DemoLong { get; set; }

            /// <summary>
            /// demolong_nullable
            /// </summary>
            public virtual Nullable<long> DemoLong_Nullable { get; set; }

            /// <summary>
            /// 构造函数勾子
            /// </summary>
            partial void HookConstructor();
        }
    }
}
